package com.freshcity.page.tree
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.tree.TreeWorldWin;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class TreeWorld extends Component
	{
		private var back:TreeWorldWin;
		private var container:MovieClip;
		private var index:int;
		private var currentTree:TreeView;
		private var total:int;
		private var trees:Array;
		
		public function TreeWorld(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
			back.nextBtn.addEventListener(MouseEvent.CLICK,onNextBtnClick);
			back.preBtn.addEventListener(MouseEvent.CLICK,onPreBtnClick);
		}
		
		protected function onPreBtnClick(event:MouseEvent):void
		{
			index--;
			renderTree();
		}
		
		protected function onNextBtnClick(event:MouseEvent):void
		{
			index++;
			renderTree();
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			back = new TreeWorldWin();
			addChild(back);
			this.container = back.container;
			currentTree = new TreeView(this.container);
		}
		
		override public function draw():void
		{
			super.draw();
		}
		
		override public function show(event:Event=null):void
		{
			EffectManager.addLoadMask(back);
			back.visible = false;
			
			currentTree.main = this.main;
			back.preBtn.visible = false;
			back.nextBtn.visible = false;
			this.index = 0;
			main.commu.send(2409,{},onReadCallback);
			super.show(event);
		}
		
		private function onReadCallback(data:*):void
		{
			//EffectManager.hideLoadMask();
			if(data == false){
				var s:String = "没有发现需要照顾的梧桐树,只好过段时间再来吧!";
				main.msg.showRollTipsMsg(s);
				main.msg.showFixRollMsg(s);
				currentTree.visible = false;
			}else{
				this.trees = data;
				this.total = this.trees.length;
				this.index = 0;
				renderTree();
			}
		}
		
		private function renderTree():void
		{
			Dispatcher.addEventListener(EventName.TreeReadDone,readTreeCallback);
			main.findTree(this.trees[index]);
			//currentTree.renderTree(o);
			back.nextBtn.visible= (this.index < (this.total-1));
			back.preBtn.visible = (this.index > 0);
		}
		
		private function readTreeCallback(evt:DataEvent):void
		{
			Dispatcher.removeEventListener(EventName.TreeReadDone,readTreeCallback);
			EffectManager.hideLoadMask();
			back.visible = true;
			var data:* = evt.data;
			currentTree.visible = true;
			var out:TreeView;
			back.nextBtn.mouseEnabled = true;
			currentTree.renderTree(data);
		}
		
		override public function destroy():void
		{
			back.nextBtn.removeEventListener(MouseEvent.CLICK,onNextBtnClick);
			back.preBtn.removeEventListener(MouseEvent.CLICK,onPreBtnClick);
			super.destroy();
		}

	}
}